<?php
namespace app\admin\controller;
use think\Response;
use app\common\controller\Base;

class Admin extends Base
{
    protected $beforeActionList = ['start'];
    public function start()
    {
        if (!$this->adminId) {
            $this->redirect($this->domain . '/'. config('admin') . '/login',301);
            exit;
        }
        $this->assign('siteInfoToJson',json_encode(config('settingInfo')));
        $this->assign('domain',config('domains'));
        $addons = db('BaseAddons')->select();
        foreach ($addons as $k => $v) {
            $addInfo[$v['name']] = $v['status'];
        }
        $this->assign('addInfo',$addInfo);
    }
    
    public function index()
    {
        $this->assign('indexTitle','首页');
        $tempIndexData = [];
        $addonsTemplatePath = ROOT_PATH . 'addons' . DS;
        $addonsList = fetch_dir($addonsTemplatePath);
        if ($addonsList) {
            foreach ($addonsList as $key => $file) {
                $tempArray = explode(DS, $file);
                $lastFileName = $tempArray[count($tempArray) - 1];
                if (preg_match('/^[a-zA-Z\s]+$/',$lastFileName)) {
                    $pageTemplatePath = ROOT_PATH . 'addons' . DS . $lastFileName . DS . 'view' . DS . 'admin' . DS . $lastFileName . '.routes';
                    if (is_file($pageTemplatePath)) {
                        $tempIndexData[] = file_get_contents($pageTemplatePath);
                    }
                }
            }
        }
        $this->assign('addonsRoutes',$tempIndexData);
        
        $tempAdminMenu = [];
        $pageTemplatePath = ROOT_PATH . 'app' . DS;
        if (is_dir($pageTemplatePath)) {
            $appList = fetch_dir($pageTemplatePath);
            foreach ($appList as $key => $file) {
                $adminMenuFilePath = $file . DS . 'adminMenu.php';
                if (is_file($adminMenuFilePath)) {
                    $tempAdminMenu[] = require $adminMenuFilePath;
                }
            }
        }
        if ($tempAdminMenu) {
            sort($tempAdminMenu);
            foreach ($tempAdminMenu as $key => $value) {
                if (isset($tempAdminMenu[$key]['path'])) {
                    $tempAdminMenu[$key]['html'] = $this->display(file_get_contents(ROOT_PATH . $tempAdminMenu[$key]['path']));
                }
                if (is_file(ROOT_PATH . $tempAdminMenu[$key]['routes'])) {
                    $tempAdminMenu[$key]['route'] = file_get_contents(ROOT_PATH . $tempAdminMenu[$key]['routes']);
                }
            }
        }
        $this->assign('AdminMenu',$tempAdminMenu);
        return $this->fetch('admin@./main');
    }
    
    public function component()
    {
        $tempComponent = '';
        $pages = [];
        $pageTemplatePath = ROOT_PATH . 'app' . DS;
        if (is_dir($pageTemplatePath)) {
            $vueList = fetch_dir($pageTemplatePath);
            foreach ($vueList as $key => $file) {
                $tempArray = explode(DS, $file);
                $lastFileName = $tempArray[count($tempArray) - 1];
                if (preg_match('/^[a-zA-Z\s]+$/',$lastFileName)) {
                    $vueFilePath = $file . DS . 'view' . DS . 'admin' . DS;
                    $vueFilePathArray = fetch_file_lists($vueFilePath);
                    if ($vueFilePathArray) {
                        foreach ($vueFilePathArray as $k => $v) {
                            if (strpos($v, '.vue') !== false) {
                                $vueArray = explode(DS, $v);
                                array_push($pages,['pagePath' => $v,'pageName' => $vueArray[count($vueArray) - 1]]);
                            }
                        }
                    }
                }
            }
        }
        if ($pages) {
            foreach ($pages as $key => $val) {
                $fileName = str_replace('.vue', '', $val['pageName']);
                $tempIndexData = $this->display(file_get_contents($val['pagePath']));
                preg_match_all('/<script>([\w\W]*)<\/script>/iU', $tempIndexData, $scriptResult);
                if ($scriptResult) {
                    preg_match_all('/<template>(.*?)<\/template>/is', $tempIndexData, $templateResult);
                    $tempComponent .= "const ".ucfirst($fileName)." = {";
                    $tempComponent .= "template: `".$templateResult[1][0]."`,";
                    $scriptResult[1][0] = str_replace('export default {', '', $scriptResult[1][0]);
                    $tempComponent .= $scriptResult[1][0];
                    $tempComponent .= "Vue.component('".ucfirst($fileName)."',".ucfirst($fileName).");";
                    $tempIndexData = '';
                    $scriptResult = '';
                    $templateResult = '';
                } 
            }
        }
        return Response::create($tempComponent)->contentType('application/x-javascript');
    }

    public function componentCss()
    {
        $tempComponent = '';
        $pages = [];
        $pageTemplatePath = ROOT_PATH . 'app' . DS;
        if (is_dir($pageTemplatePath)) {
            $cssList = fetch_dir($pageTemplatePath);
            foreach ($cssList as $key => $file) {
                $tempArray = explode(DS, $file);
                $lastFileName = $tempArray[count($tempArray) - 1];
                if (preg_match('/^[a-zA-Z\s]+$/',$lastFileName)) {
                    $cssFilePath = $file . DS . 'view' . DS . 'admin' . DS;
                    $cssFilePathArray = fetch_file_lists($cssFilePath);
                    if ($cssFilePathArray) {
                        foreach ($cssFilePathArray as $k => $v) {
                            if (strpos($v, '.vue') !== false) {
                                $cssArray = explode(DS, $v);
                                array_push($pages,['pagePath' => $v,'pageName' => $cssArray[count($cssArray) - 1]]);
                            }
                        }
                    }
                }
            }
        }
        if ($pages) {
            foreach ($pages as $key => $val) {
                $fileName = str_replace('.vue', '', $val['pageName']);
                $tempIndexData = $this->display(file_get_contents($val['pagePath']));
                preg_match_all('/<script>([\w\W]*)<\/script>/iU', $tempIndexData, $scriptResult);
                if ($scriptResult) {
                    preg_match_all('/<style>([\w\W]*)<\/style>/iU', $tempIndexData, $templateResult);
                    $tempComponent .=  $templateResult[1][0];
                } 
            }
        }
        return Response::create($tempComponent)->contentType('text/css');
    }

    public function addonsJs()
    {
        $tempComponent = '';
        $pages = [];
        $addonsTemplatePath = ROOT_PATH . 'addons' . DS;
        $addonsList = fetch_dir($addonsTemplatePath);
        if ($addonsList) {
            foreach ($addonsList as $key => $file) {
                $tempArray = explode(DS, $file);
                $lastFileName = $tempArray[count($tempArray) - 1];
                $pageTemplatePath = ROOT_PATH . 'addons' . DS . $lastFileName . DS . 'view' . DS . 'admin' . DS;
                $routesPath = ROOT_PATH . 'addons' . DS . $lastFileName . DS . 'view' . DS . 'admin' . DS . $lastFileName . '.routes';
                if (is_dir($pageTemplatePath) && is_file($routesPath)) {
                    $addonsList = fetch_file_lists($pageTemplatePath);
                    foreach ($addonsList as $file) {
                        if (strpos($file, '.vue') !== false && (strpos($file, DS . 'src') === false)) {
                            $tempArray = explode(DS, $file);
                            array_push($pages,['pagePath' => $file,'pageName' => $tempArray[count($tempArray) - 1]]);
                        }
                    }
                }
            }
        }
        if ($pages) {
            foreach ($pages as $key => $val) {
                $fileName = str_replace('.vue', '', $val['pageName']);
                $tempIndexData = $this->display(file_get_contents($val['pagePath']));
                preg_match_all('/<script>([\w\W]*)<\/script>/iU', $tempIndexData, $scriptResult);
                if ($scriptResult) {
                    preg_match_all('/<template>(.*?)<\/template>/is', $tempIndexData, $templateResult);
                    $tempComponent .= "const ".ucfirst($fileName)." = {";
                    $tempComponent .= "template: `".$templateResult[1][0]."`,";
                    $scriptResult[1][0] = str_replace('export default {', '', $scriptResult[1][0]);
                    $tempComponent .= $scriptResult[1][0];
                    $tempComponent .= "Vue.component('".ucfirst($fileName)."',".ucfirst($fileName).");";
                    $tempIndexData = '';
                    $scriptResult = '';
                    $templateResult = '';
                } 
            }
        }
        return Response::create($tempComponent)->contentType('application/x-javascript');
    }
    public function addonsCss()
    {
        $tempComponent = '';
        $pages = [];
        $addonsTemplatePath = ROOT_PATH . 'addons' . DS;
        $addonsList = fetch_dir($addonsTemplatePath);
        if ($addonsList) {
            foreach ($addonsList as $key => $file) {
                $tempArray = explode(DS, $file);
                $lastFileName = $tempArray[count($tempArray) - 1];
                $pageTemplatePath = ROOT_PATH . 'addons' . DS . $lastFileName . DS . 'view' . DS . 'admin' . DS;
                $routesPath = ROOT_PATH . 'addons' . DS . $lastFileName . DS . 'view' . DS . 'admin' . DS . $lastFileName . '.routes';
                if (is_dir($pageTemplatePath) && is_file($routesPath)) {
                    $addonsList = fetch_file_lists($pageTemplatePath);
                    foreach ($addonsList as $file) {
                        if (strpos($file, '.vue') !== false && (strpos($file, DS . 'src') === false)) {
                            $tempArray = explode(DS, $file);
                            array_push($pages,['pagePath' => $file,'pageName' => $tempArray[count($tempArray) - 1]]);
                        }
                    }
                }
            }
        }
        if ($pages) {
            foreach ($pages as $key => $val) {
                $fileName = str_replace('.vue', '', $val['pageName']);
                $tempIndexData = $this->display(file_get_contents($val['pagePath']));
                preg_match_all('/<script>([\w\W]*)<\/script>/iU', $tempIndexData, $scriptResult);
                if ($scriptResult) {
                    preg_match_all('/<style>([\w\W]*)<\/style>/iU', $tempIndexData, $templateResult);
                    $tempComponent .=  $templateResult[1][0];
                } 
            }
        }
        return Response::create($tempComponent)->contentType('text/css');
    }
}
